package com.ymatou.iapi.productmanager.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.packageInfo.CatalogPackageInfoDto;
import com.ymatou.iapi.productmanager.parameter.packageInfo.PackageInfoListBean;
import com.ymatou.iapi.productmanager.parameter.packageInfo.ProductPackageDto;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.AddSellerProductCall;
import com.ymatou.iapi.productmanager.service.GetPackageInfoByCatalogIdListCall;
import com.ymatou.iapi.productmanager.service.SaveCatalogPackageListCall;
import com.ymatou.iapi.sellerproduct.parameter.CatalogLevelBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddProductCallService;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.AssertHelper;


public class Ts_GetPackageInfoByCatalogIdList {
	private static String sellerName = EnvSetup.getData("productsellername");
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("规格套装查询");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetPackageInfoByCatalogIdListCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPackageInfoByCatalogIdList_001() {
		Logger.start(true, "多规格多个套装查询");
		try{
			SaveCatalogPackageListCall saveCatalogPackageListCall = new SaveCatalogPackageListCall();
			int packageId1 = saveCatalogPackageListCall.callAndGetReturnData(MapUtil.hashMap("ProductPackageList[0]->SubCatalogName","测试组合001测试组合001",
																	"ProductPackageList[0]->Price",20,"ProductPackageList[0]->Num",30,
																	"ProductPackageList[0]->Sku","test","ProductPackageList[0]->ProductCode","procodetest"))
									.getJSONObject("Data").getJSONArray("SubCatalogIdList").getInt(0);
			
			int packageId2 = saveCatalogPackageListCall.callAndGetReturnData(MapUtil.hashMap("ProductPackageList[0]->SubCatalogName","测试组合002测试组合001"))
					.getJSONObject("Data").getJSONArray("SubCatalogIdList").getInt(0);
			
			
			int packageId3 = saveCatalogPackageListCall.callAndGetReturnData(MapUtil.hashMap("ProductPackageList[0]->SubCatalogName","测试组合003测试组合001"))
					.getJSONObject("Data").getJSONArray("SubCatalogIdList").getInt(0);

			int packageId4 = saveCatalogPackageListCall.callAndGetReturnData(MapUtil.hashMap("ProductPackageList[0]->SubCatalogName","测试组合004测试组合001"))
				.getJSONObject("Data").getJSONArray("SubCatalogIdList").getInt(0);
			
			AddSellerProductRequest addProdReq = AddProductCallService.getAddSellerProductRequestFromFile("多规格带SPU", 
					MapUtil.hashMap("sellerId",sellerId,"sellerName",sellerName));
			
			//带上套装
			addProdReq.getProduct().getCatalogList().get(0).setSubCatalogIdList((Arrays.asList(packageId1,packageId2)));
			addProdReq.getProduct().getCatalogList().get(0).setPackage(true);
			
			addProdReq.getProduct().getCatalogList().get(1).setSubCatalogIdList((Arrays.asList(packageId3,packageId4)));
			addProdReq.getProduct().getCatalogList().get(1).setPackage(true);
			
			JSONObject ret = new AddSellerProductCall().callAndGetReturnData(addProdReq);
			String productId = ret.getJSONObject("Data").getString("ProductId");
		
			AddProductCallService.checkAddProduct(productId, addProdReq);
			
			
			Map<String,List<Integer>> catalogAndSubCatalogIdMap = MapUtil.hashMap(addProdReq.getProduct().getCatalogList().get(0).getCatalogId(),Arrays.asList(packageId1,packageId2),
																				  addProdReq.getProduct().getCatalogList().get(1).getCatalogId(),Arrays.asList(packageId3,packageId4));
		
			checkGetPackageInfoByCatalogIdList(catalogAndSubCatalogIdMap);
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPackageInfoByCatalogIdList_002() {
		Logger.start(true, "规格不存在对应套装");
		try{
			AddSellerProductRequest addProdReq = AddProductCallService.getAddSellerProductRequestFromFile("多规格", 
					MapUtil.hashMap("sellerId",sellerId,"sellerName",sellerName));
			
			
			JSONObject ret = new AddSellerProductCall().callAndGetReturnData(addProdReq);
			String productId = ret.getJSONObject("Data").getString("ProductId");
			
			AddProductCallService.checkAddProduct(productId, addProdReq);
			
			Map<String,List<Integer>> catalogAndSubCatalogIdMap = MapUtil.hashMap(addProdReq.getProduct().getCatalogList().get(0).getCatalogId(),new ArrayList<>());

			checkGetPackageInfoByCatalogIdList(catalogAndSubCatalogIdMap);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPackageInfoByCatalogIdList_003() {
		Logger.start(true, "规格套装部分存在，部分不存在");
		try{
			SaveCatalogPackageListCall saveCatalogPackageListCall = new SaveCatalogPackageListCall();
			int packageId1 = saveCatalogPackageListCall.callAndGetReturnData(MapUtil.hashMap("ProductPackageList[0]->SubCatalogName","测试组合001测试组合001",
																	"ProductPackageList[0]->Price",20,"ProductPackageList[0]->Num",30,
																	"ProductPackageList[0]->Sku","test","ProductPackageList[0]->ProductCode","procodetest"))
									.getJSONObject("Data").getJSONArray("SubCatalogIdList").getInt(0);
			
			int packageId2 = saveCatalogPackageListCall.callAndGetReturnData(MapUtil.hashMap("ProductPackageList[0]->SubCatalogName","测试组合002测试组合001"))
					.getJSONObject("Data").getJSONArray("SubCatalogIdList").getInt(0);
			
			AddSellerProductRequest addProdReq = AddProductCallService.getAddSellerProductRequestFromFile("多规格带SPU", 
					MapUtil.hashMap("sellerId",sellerId,"sellerName",sellerName));
			
			//带上套装
			addProdReq.getProduct().getCatalogList().get(0).setSubCatalogIdList((Arrays.asList(packageId1,packageId2)));
			addProdReq.getProduct().getCatalogList().get(0).setPackage(true);
			
			JSONObject ret = new AddSellerProductCall().callAndGetReturnData(addProdReq);
			String productId = ret.getJSONObject("Data").getString("ProductId");
		
			AddProductCallService.checkAddProduct(productId, addProdReq);
			
			
			Map<String,List<Integer>> catalogAndSubCatalogIdMap = MapUtil.hashMap(addProdReq.getProduct().getCatalogList().get(0).getCatalogId(),Arrays.asList(packageId1,packageId2),
																				  addProdReq.getProduct().getCatalogList().get(1).getCatalogId(),new ArrayList<>());
		
			checkGetPackageInfoByCatalogIdList(catalogAndSubCatalogIdMap);
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	public static void checkGetPackageInfoByCatalogIdList(Map<String,List<Integer>> catalogAndSubCatalogIdMap) throws Exception{
		List<String> catalogIds = new ArrayList<>(catalogAndSubCatalogIdMap.keySet());
		
		JSONObject ret = new GetPackageInfoByCatalogIdListCall().callAndGetReturnData(MapUtil.hashMap("CatalogIdList",new JSONArray(catalogIds)));
		
		Logger.verifyEquals(200, ret.getInt("Code"), "return code");
		
		PackageInfoListBean actBean = ProductManagerCallService.getBeanFromJson(ret.getJSONObject("Data"), PackageInfoListBean.class);
	
		PackageInfoListBean tgtBean = getPackageInfoListBean(catalogAndSubCatalogIdMap);
		
		AssertHelper.assertResultEqual(tgtBean, actBean, "checkGetPackageInfoByCatalogIdList");
	
	
	
	}
	
	
	public static PackageInfoListBean getPackageInfoListBean(Map<String,List<Integer>> catalogAndSubCatalogIdMap) throws InterruptedException{
		PackageInfoListBean tgtBean = new PackageInfoListBean();
		List<CatalogPackageInfoDto> packageInfoList = new ArrayList<>();
		for(String catalogId : catalogAndSubCatalogIdMap.keySet()){
			if (catalogAndSubCatalogIdMap.get(catalogId)==null||catalogAndSubCatalogIdMap.get(catalogId).size()==0){
				//do nothing
			}else{
				List<Integer> subCatalogIds = catalogAndSubCatalogIdMap.get(catalogId);
				CatalogPackageInfoDto packageInfo = new CatalogPackageInfoDto();
				packageInfo.setCatalogId(catalogId);
				
				List<ProductPackageDto> productPackages = new ArrayList<>();
				for(int subCatalogId:subCatalogIds){
					Map<String,Object> subCatalogMap = intergratedProductMapper.selectSubCatalogBySubCatalogId(subCatalogId).get(0);
					
					ProductPackageDto productPackage = new ProductPackageDto();
					productPackage.setCatalogId(catalogId);
					productPackage.setSubCatalogId(subCatalogId);
					productPackage.setSubCatalogName(String.valueOf(subCatalogMap.get("SubCatalogName")));
					productPackage.setNum((int)subCatalogMap.get("Num"));
					productPackage.setPrice(new BigDecimal(subCatalogMap.get("Price").toString()).doubleValue());
					productPackage.setProductCode(subCatalogMap.get("ProductCode")==null?null:subCatalogMap.get("ProductCode").toString());
					productPackage.setSku(subCatalogMap.get("Sku")==null?null:subCatalogMap.get("Sku").toString());
					
					CatalogLevelBean levelBean = SellerProductCallService.getProductCategroyByThirdCategoryId((int)subCatalogMap.get("ThirdCategoryId"));
					productPackage.setThirdCategoryId(levelBean.getThirdCategoryId());
					productPackage.setSecondCategoryId(levelBean.getSecondCategoryId());
					productPackage.setCategoryId(levelBean.getMasterCategoryId());
					
					productPackages.add(productPackage);
				}
				packageInfo.setPackageInfoList(productPackages);
				packageInfoList.add(packageInfo);
			}
			
			
		}
		
		
		tgtBean.setProductPackageList(packageInfoList);
		return tgtBean;
	}

}
